package com.comp7940.groupf.tgbot.controller;

import com.comp7940.groupf.tgbot.builder.ResultBuilder;
import com.comp7940.groupf.tgbot.entity.AccInfo;
import com.comp7940.groupf.tgbot.entity.common.Result;
import com.comp7940.groupf.tgbot.mapper.AccInfoMapper;
import com.comp7940.groupf.tgbot.redis.RedisCache;
import com.comp7940.groupf.tgbot.service.AccInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController                 // 返回json格式的数据
@CrossOrigin                    // 允许跨域
@RequestMapping("api/acc-info")   // 下级目录
public class AccInfoController {
    @Autowired
    private AccInfoMapper aim;
    @Autowired
    private AccInfoService ais;
    @Autowired
    private RedisCache redisCache;
    @PostMapping("add")
    public Result<Boolean> add(@RequestBody AccInfo args) {
        aim.insert(args);
        return ResultBuilder.buildSuccess(null);
    }
    @PostMapping("del")
    public Result<Boolean> del(@RequestBody Map<String, Object> args) {
        if (redisCache.getCacheObject((String) args.get("tgacc")) == null || redisCache.getCacheObject((String) args.get("tgacc")).equals(false))
            return ResultBuilder.buildFail(501, "Permission Denied");
        aim.deleteById((String) args.get("id"));
        return ResultBuilder.buildSuccess(null);
    }
    @PostMapping("ps")
    public Result<List<AccInfo>> ps(@RequestBody Map<String, Object> args) {
        return ResultBuilder.buildSuccess(ais.ps(args));
    }

    @PostMapping("update")
    public Result<List<AccInfo>> update(@RequestBody AccInfo args) {
        aim.updateById(args);
        return ResultBuilder.buildSuccess(null);
    }
}
